// 显示提示信息
function divMsg(dom, result, classType, stayMilliSecond){
	$(dom).text(result);
	$(dom).addClass(classType);
	$(dom).show();

	if (null == stayMilliSecond || undefined == stayMilliSecond)
		stayMilliSecond = 6000;
	$(dom).fadeOut(stayMilliSecond);
}


function getInitialPageSize() {
	var cookiePageSize = $.cookie("warnPageSize");
	if (cookiePageSize)
		return parseInt(cookiePageSize);

	return 10;
}


var lastQueryOffset = 0, lastQueryLimit = 0;
var responseHandlerFunc = function(res) {
	return {"code": "0", "rows": res.elements, "total": res.totalCount};
}
var queryParamsFunc = function(params) {
	params["messageStatus"] = document.getElementById("messageStatus").value;
	params["orderBy"] = "create_time";
	params["desc"] = true;

	lastQueryOffset = params["offset"];
	lastQueryLimit = params["limit"];
	return params;
}

function sendMessage(id, status) {
	var callback = function(response) {
		if (0 == response.code)
			divMsg('#dataMsg','发送成功','alert-success');
		else
			divMsg('#dataMsg',response.message,'alert-success');
	};

	var urlPrefix;
	if (status == 3) {
		urlPrefix = "../message/reliveAndSendMessage/";
	} else {
		urlPrefix = "../message/sendMessage/";
	}

	$.post(urlPrefix + id, {}, callback);
}
function updateMessageContent(id) {
	var callback = function(response) {
		if (0 == response.code)
			divMsg('#dataMsg','更新成功','alert-success');
		else
			divMsg('#dataMsg',response.message,'alert-success');
	};

	var messageContent = document.getElementById("content"+id).value;
	$.post("../message/updateMessageContent/" + id, {"messageContent":messageContent}, callback);
}
function removeMessage(id) {
	var removeConfirm = confirm("删除消息["+id+"]吗？");
	if (removeConfirm == true) {
	} else {
		return;
	}

	var callback = function(response) {
		if (0 == response.code)
			divMsg('#dataMsg','删除成功','alert-success');
		else
			divMsg('#dataMsg',response.message,'alert-success');
	};
	$.post("../message/removeMessage/" + id, {}, callback);
}

var timeFormatter = function(value,r,index) {
    var date = new Date();
    date.setTime(value);

    return date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate()
        + "_" + date.getHours() + ":" + date.getMinutes()  + ":" + date.getSeconds();
}
var btnFormatter = function(value,r,index) {
	var str = "";
	if (r.status == 3 || (r.status == 2 && r.retry >= 2)) {
		str  = "<input type=\"button\" onclick=\"sendMessage('" + r.id + "'," + r.status + ")\" value=\"重试\">"
		+ "<br><br>"
		+ "<input type=\"button\" onclick=\"updateMessageContent('" + r.id + "')\" value=\"修改内容\">";
	}

	if (r.status == 3) {
		str = str
			+ "<br><br>"
			+ "<input type=\"button\" onclick=\"removeMessage('" + r.id + "')\" value=\"删除\">";
	}

	return str;
}
var longTextFormatter70 = function(value,r,index) {
    return '<textarea id="content' + r.id + '" rows="10" cols="70">' + value + '</textarea>';
}
var longTextFormatter140 = function(value,r,index) {
	return '<textarea rows="10" cols="140">' + value + '</textarea>';
}

//表格展示部分
$("#messageTable").bootstrapTable({
	url:"../message/page",
	method: "get", //请求方式（*）
	striped: true, //是否显示行间隔色
	cache: false, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
	pagination: true, //是否显示分页（*）
	sidePagination: "server", //分页方式：client客户端分页，server服务端分页（*）
	pageNumber: 1,
	contentType: "text/plain;charset=utf8",
	pageSize: getInitialPageSize(),
	pageList: [10, 15, 25, 50, 100, '所有'],
	smartDisplay: false,//智能显示分页按钮
	search: false,
	strictSearch: true,
	showColumns: true, //是否显示所有的列
	showRefresh: true, //是否显示刷新按钮
	minimumCountColumns: 2, //最少允许的列数
	clickToSelect: true, //是否启用点击选中行
	showToggle: true, //是否显示详细视图和列表视图的切换按钮
	cardView: false, //是否显示详细视图
	detailView: false, //是否显示父子表
	sortable: true, //是否启用排序
	queryParams: queryParamsFunc,
	responseHandler: responseHandlerFunc,
	columns:[[
		{field: 'id', title: '序号',visible:true,
			formatter: function (value, row, index) {
				return index+1;
			}},
		{field:'id',title:'id'},
		{field:'source',title:'发送者'},
		{field:'destination',title:'消息队列'},
		{field:'retry',title:'重发次数'},
		{field:'status',title:'消息状态'},
		{field:'createTime',title:'创建时间',formatter:timeFormatter},
		{field:'updateTime',title:'更新时间',formatter:timeFormatter},
		{field:'status',title:'',formatter:btnFormatter},
		{field:'traceId',title:'traceId'},
		{field:'content',title:'消息内容',formatter:longTextFormatter70},
		{field:'errorLog',title:'错误日志',formatter:longTextFormatter140}
	]]
});

